package util.coordTranslate;

import java.awt.Dimension;
import java.awt.Point;

/**
 * Converts a given Java dimension where (0,0) is in the top left to Cartesian
 * where (0,0) is in the center. Also inverts so down is up and up is down.
 * 
 * @author Nathan Klug
 */
public class CartesianCoordinateSystem extends AbstractCoordinateSystem
{

    Point myOrigin;

    /**
     * Cartesian coordinate systems have an origin
     * 
     * @param origin
     */
    public CartesianCoordinateSystem(Point origin)
    {
        myOrigin = origin;
    }

    /**
     * Constructor which automatically places the origin at the center
     * 
     * @param size
     *            the size of the canvas
     */
    public CartesianCoordinateSystem(Dimension size)
    {
        myOrigin = new Point(size.width / 2, size.height / 2);
    }

    /**
     * Translates from java's default display coordinates to the equivalent
     * coordinates in the Cartesian system.
     */
    public Point translateCoordinates(Point toConvert)
    {
        return new Point(myOrigin.x + toConvert.x, myOrigin.y - toConvert.y);
    }

    /**
     * Translates from java's default display coordinates to the equivalent
     * coordinates in the Cartesian system.
     */
    public Point translateCoordinates(double x, double y)
    {
        return translateCoordinates(new Point((int) x, (int) y));
    }

    /**
     * Used for things like resizing the window, etc.
     */
    public void setOriginAtCenter(Dimension size)
    {
        myOrigin = new Point(size.width / 2, size.height / 2);
    }
}
